﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using DWH.PP;
using System.Timers;
using TH =System.Threading;

namespace RDService
{
    public partial class RDService : ServiceBase
    {
        private PPTaskExecutor downloader;
        private Timer taskTimer;
        private DateTime lastRun;
        private bool enableAutoRestart = false;

        public RDService()
        {
            InitializeComponent();            

            downloader = new PPTaskExecutor();            
        }

        protected override void OnStart(string[] args)
        {
            //TH.Thread.Sleep(1000 * 15);
            downloader.InitializeTasks();
            downloader.StartTasks();
            taskTimer = new Timer(2 * 60 * 60 * 1000);//every 2 hours
            taskTimer.Elapsed += new ElapsedEventHandler(taskTimerElapsed);
            taskTimer.Start();
        }

        protected override void OnStop()
        {
            downloader.StopTasks();
        }

        private void taskTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (enableAutoRestart)
            {
                taskTimer.Stop();
                downloader.ResetTasks();
                taskTimer.Start();
            }
        }
    }
}
